package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.common.enums.Code;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.common.util.CurrentUserContextHolders;
import com.qk.dto.business.BusinessPageDTO;
import com.qk.dto.business.BusinessPoolPageDTO;
import com.qk.dto.business.BusinessUpdateDTO;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.management.mapper.BusinessMapper;
import com.qk.management.mapper.BusinessTrackRecordMapper;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessDataVO;
import com.qk.vo.business.BusinessPageVO;
import com.qk.vo.business.BusinessPoolPageVO;
import com.qk.vo.clue.CluePoolVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 *
 * @author MrZhao
 * @created 2025-10-15 10:53
 * @description 商机业务实现类
 *
 */
@Slf4j
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {

    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;



    @Override
    public PageResult<BusinessPoolPageVO> getBusinessPoolPageList(BusinessPoolPageDTO dto) {

        System.out.println(dto);
        Page<BusinessPoolPageVO> list = this.baseMapper.selectBusinessPoolPageList(new Page<>(dto.getPage(),dto.getPageSize()),dto);

        return PageResult.<BusinessPoolPageVO>builder()
                .total(list.getTotal())
                .rows(list.getRecords())
                .build();
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateBusiness(BusinessUpdateDTO dto) {
        if (BeanUtil.isEmpty(dto)||dto.getName()==null ||dto.getStatus()==null){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        Business business = BeanUtil.copyProperties(dto, Business.class);
        if (business.getStatus().equals(BusinessStatusConstants.WAIT_FOLLOW_UP))
            business.setStatus(BusinessStatusConstants.FOLLOW_UP);
        this.updateById(business);
        BusinessTrackRecord track = BeanUtil.copyProperties(dto, BusinessTrackRecord.class);


        track.setId(null);
        track.setUserId(CurrentUserContextHolders.get());
        track.setBusinessId(dto.getId());
        track.setCreateTime(LocalDateTime.now());
        this.businessTrackRecordMapper.insert(track);
    }

    @Override
    public BusinessDataVO getBusinessById(Integer id) {

        BusinessDataVO vo = BeanUtil.copyProperties(this.baseMapper.selectById(id), BusinessDataVO.class);
        List<BusinessTrackRecord> trackRecords = this.businessTrackRecordMapper.selectBusinessTrackRecordMapperList(id);
        if (trackRecords!=null&& trackRecords.size()>0)
            vo.setTrackRecords(trackRecords);
        return vo;
    }

    @Override
    public void backToPool(Integer id) {
        if (Objects.isNull(id))
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        this.baseMapper.updateBackToPool(Business.builder()
                .id(id)
                .status(BusinessStatusConstants.WAIT_ALLOCATION)
                .userId(null)
                .updateTime(LocalDateTime.now())
                .build()
        );
    }
  @Transactional(rollbackFor = Exception.class)
    @Override
    public void toCustomer(Integer id) {
        if (Objects.isNull(id))
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        this.baseMapper.updateById(Business.builder()
                .id(id)
                .status(BusinessStatusConstants.CONVERT_TO_CUSTOMER)
                .updateTime(LocalDateTime.now())
                .build()
        );
    }


    @Override
    public void saveBusiness(Business business) {
        if (BeanUtil.isEmpty(business) || Objects.isNull(business.getName()) || Objects.isNull(business.getPhone())) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        business.setStatus(BusinessStatusConstants.WAIT_ALLOCATION);
        this.save(business);
    }

    @Override
    public PageResult<BusinessPageVO> businessPageList(BusinessPageDTO dto) {
        Page<BusinessPageVO> list = this.baseMapper.selectBusinessPageList(new Page<>(dto.getPage(), dto.getPageSize()), dto);
        return PageResult.<BusinessPageVO>builder()
                .total(list.getTotal())
                .rows(list.getRecords())
                .build();
    }

}